/*
 * @author Ihsan Al-Hamoud
 * @Web www.al-hamoud.com
 * @Web www.patternsexpert.com
 * @version 1.0
 * If you need a completed Blogger with GWT
 * as Client side. Please email me on
 * ihsan.alhamoud@gmail.com
 * I will be very happy to help.
 */

package com.patternsexpert.blog.client.entities;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(detachable="true")
public class Article {
	
	@NotPersistent
	private static final int LIMIT = 400;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	
	@Persistent
	private String spam;
	
	@Persistent
	private String published;
	
	@NotPersistent
	private String description;
	
	@Persistent
	private List<String> wDes;
	
	@Persistent
	private List<String> wText;
	
	@NotPersistent
	private String text;
	
	@Persistent
	private String title;
	
	@Persistent
	private Date date;
	
	@Persistent
	private String email;
	
	@Persistent
	private String category;
	
	public void setDescriptoin(String des){
		this.description = des;
		this.insertDes();
	}
	
	public String getDescription() {
		StringBuilder mt = new StringBuilder();
		for(String result : this.wDes){
            mt.append(result);
        }
        return mt.toString();
	}
	
	public void setText(String details)
	{
		this.text = details;
		this.insertText();
	}
	
	public String getText()
	{
        StringBuilder mt = new StringBuilder();
        for(String result : this.wText){
            mt.append(result);
    
        }
        return mt.toString();
	}
	
	
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTitle() {
		return title;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getId() {
		return id;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public Date getDate() {
		return date;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getEmail() {
		return email;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String getCategory() {
		return category;
	}
	public String getArticleLinkAsString(){
		return this.category + "?article=" + this.id.toString();
	}
	
	private void insertDes(){
        StringBuilder temp = new StringBuilder();

        int section = this.description.length()/LIMIT;
        int lastBegin = section * LIMIT;

        temp.append(this.description);
        String last = this.description.substring(lastBegin);
        
        this.wDes = new ArrayList<String>();

        for(int i=0;i< lastBegin;i +=LIMIT)
        {
            this.wDes.add(temp.substring(i, i+LIMIT));
        }

        this.wDes.add(last);
		
	}// end of insertDes
	
    private void insertText(){

        StringBuilder temp = new StringBuilder();

        int section = this.text.length()/LIMIT;
        int lastBegin = section * LIMIT;

        temp.append(this.text);
        String last = this.text.substring(lastBegin);
        
        this.wText = new ArrayList<String>();

        for(int i=0;i< lastBegin;i +=LIMIT)
        {
            this.wText.add(temp.substring(i, i+LIMIT));
        }

        this.wText.add(last);

    }

	public void setSpam(String spam) {
		this.spam = spam;
	}

	public String isSpam() {
		return spam;
	}

	public void setPublished(String published) {
		this.published = published;
	}

	public String isPublished() {
		return published;
	}
	
}
